package org.megha.blog.example.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

/**
 * Reads all lines from a {@link Reader}.
 * Don't use this to read huge files!
 *
 * @author megha birmiwal
 */
public class LinesReader {

	private Reader reader;
	
	public LinesReader(Reader reader) {
		this.reader = reader;
	}

	/**
	 * Reads all the lines from the provided {@link Reader}.
	 * 
	 * @return a list of strings (lines from the file)
	 * @throws IOException if there was an exception reading from the file
	 */
	public List<String> readAllLines() throws IOException {
		BufferedReader bufferedReader = new BufferedReader(reader);
		List<String> lines = new ArrayList<String>();
		String line = bufferedReader.readLine();
		while (line != null) {
			lines.add(line);
			line = bufferedReader.readLine();
		}
		return lines;
	}
}
